package de.reminder.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.layout.client.Layout.Alignment;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;

import de.reminder.client.MySimpleReminder;
import de.reminder.client.event.InitializedEvent;
import de.reminder.client.event.InitializedEventHandler;
import de.reminder.shared.model.FacebookUser;

public class EditUsers extends DialogBox{
	private AbsolutePanel wrapper;
	private HorizontalPanel boundary;
	private HorizontalPanel content;
	private Image imageEndLeft;
	private Image imageEndRight;
	private Image imageEdge;
	private PushButton buttonAdd;
	public static MultiWordSuggestOracle oracle;
	
	private ReminderView parent;
	
	public EditUsers(ReminderView parent){
		this.parent = parent;
		
		wrapper = new AbsolutePanel();
		boundary = new HorizontalPanel();
		content = new HorizontalPanel();
		content.setVerticalAlignment(HasAlignment.ALIGN_BOTTOM);
		imageEndLeft = new Image("images/addPerson/rahmen_links.png");
		imageEndRight = new Image("images/addPerson/rahmen_rechts.png");
		imageEdge = new Image("images/addPerson/rahmen_ecke.png");
		
		buttonAdd = new PushButton(new Image("images/addPerson/buttonAdd.png"));
		buttonAdd.getElement().setClassName("");
		buttonAdd.addClickHandler(new AddTileClickHandler(buttonAdd));
		
		boundary.add(imageEndLeft);
		boundary.add(content);
		boundary.add(imageEndRight);
		
		wrapper.add(boundary);
		wrapper.add(imageEdge,30,146);
		wrapper.setHeight("170px");
		wrapper.getElement().setId("EditUser");
		
		content.getElement().setId("EditUserContent");
		content.setHeight("150px");
		content.add(buttonAdd);
		this.setWidget(wrapper);
		this.setAutoHideEnabled(true);
		this.setModal(true);
		this.getElement().setClassName("");
		
		this.setPopupPosition(400, 150);		
	}
	
	private class AddTileClickHandler implements ClickHandler{
		private PushButton source;
		
		public AddTileClickHandler(PushButton source){
			this.source = source;
		}
		
		@Override
		public void onClick(ClickEvent event) {
			EditUsers.this.content.remove(source);
			UserTile tile = new UserTile(EditUsers.this);
			tile.setSuggesting(oracle);
			EditUsers.this.content.add(tile);
		}
	}

	public void setInputFinished(FacebookUser u) {
		content.add(buttonAdd);
		parent.addUser(u);
	}

	public static void onInitialized() {
		oracle = new MultiWordSuggestOracle();
		for(FacebookUser u : MySimpleReminder.currentUser.getFriends()){
			oracle.add(u.getName());
		}
	}
	
}
